package org.luxor.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.luxor.commons.core.validator.group.AddGroup;
import org.luxor.commons.core.validator.group.UpdateGroup;
import org.luxor.commons.core.web.controller.BaseRestController;
import org.luxor.commons.core.web.data.R;
import org.luxor.commons.jdbc.metadata.PageQuery;
import org.luxor.system.entity.SysRoleEntity;
import org.luxor.system.entity.SysUserRoleRefEntity;
import org.luxor.system.service.ISysRoleService;
import org.luxor.system.service.ISysUserRoleRefService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.stream.Collectors;

/**
 * 用户与用户组对应关系
 *
 * @author Mark @date 2020-12-29 10:27:12
 */
@RestController
@RequestMapping("/system/sysUserRoleRef")
@Api(tags = "用户与用户组对应关系")
public class SysUserRoleRefController extends BaseRestController {

    @Resource
    private ISysUserRoleRefService sysUserRoleRefService;
    @Resource
    private ISysRoleService sysRoleService;

    @ApiOperation("分页列表")
    @PostMapping("/list")
    public R<IPage<SysUserRoleRefEntity>> list(@RequestBody PageQuery<HashMap> searchPage) {
        IPage<SysUserRoleRefEntity> data = sysUserRoleRefService.page(searchPage.getPage(), searchPage.getSearch());
        return R.ok(data);
    }

    @ApiOperation("详情信息")
    @ApiImplicitParam(name = "id", value = "主键ID", paramType = "path", required = true)
    @GetMapping("/info/{id}")
    public R<SysUserRoleRefEntity> info(@PathVariable("id") Long id) {
        SysUserRoleRefEntity sysUserGroupRef = sysUserRoleRefService.getById(id);
        return R.ok(sysUserGroupRef);
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public R<SysUserRoleRefEntity> save(@Validated(AddGroup.class) @RequestBody SysUserRoleRefEntity sysUserGroupRef) {
        sysUserRoleRefService.save(sysUserGroupRef);
        return R.ok(sysUserGroupRef);
    }

    @ApiOperation("修改")
    @PutMapping("/update")
    public R<SysUserRoleRefEntity> update(@Validated(UpdateGroup.class) @RequestBody SysUserRoleRefEntity sysUserGroupRef) {
        sysUserRoleRefService.updateById(sysUserGroupRef);
        return R.ok(sysUserGroupRef);
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        sysUserRoleRefService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @ApiOperation("用户关联的角色列表")
    @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "path", required = true)
    @GetMapping("/{userId}/roles")
    public R<Collection<SysRoleEntity>> menus(@PathVariable("userId") Long userId) {
        Collection<SysUserRoleRefEntity> userRoleRefs = sysUserRoleRefService.listByUserId(userId);
        Collection<Long> roleIds = userRoleRefs.stream().map(SysUserRoleRefEntity::getRoleId).collect(Collectors.toSet());
        Collection<SysRoleEntity> roles = sysRoleService.listByIds(roleIds);
        return R.ok(roles);
    }

}
